import axios from "axios";
import { ElMessage } from "element-plus";

import { Store } from "pinia";

let url = "http://localhost:8888";

const request = axios.create({
  baseURL: url,
  timeout: 5000,
});
declare module "axios" {
  interface AxiosResponse<T = any> {
    // 这个地方放属性
    message: any;
  }
  export function create(config?: AxiosRequestConfig): AxiosInstance;
}

request.interceptors.request.use(
  (config) => {
    // console.log(Store.getters.GET_TOKEN);
    config.headers.token = localStorage.getItem("GET_TOKEN");
    return config;
  },
  (error) => {
    console.log(error);
  }
);

request.interceptors.response.use(
  (response) => {
    const res = response.data;
    return res;
  },
  (error) => {
    ElMessage({
      message: "请求出错",
      type: "error",
      duration: 3000,
    });
  }
);
export default request;
